package com.deep.transaction.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

@Service
public class TestService {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED, timeout = 3)
    public int timeout(int timeout, int userId) throws InterruptedException {
        jdbcTemplate.execute("DELETE FROM user where id = " + userId);
        TimeUnit.SECONDS.sleep(timeout);
        jdbcTemplate.execute("SELECT 1");
        return timeout;
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED, timeout = 3)
    public int timeout2(int timeout, int userId) throws InterruptedException {
        jdbcTemplate.execute("DELETE FROM user where id = " + userId);
        TimeUnit.SECONDS.sleep(timeout);
        return timeout;
    }
}
